﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;

namespace TomatoClock.Utilities
{
    public class PomodoroTimer
    {
        private Timer timer;
        private int timeLeft;
        private bool isPaused;
        private bool isRunning = false;

        public event Action<int>? TimeChanged;
        public event Action? TimerFinished;

        public PomodoroTimer()
        {
            timer = new Timer();
            timer.Interval = 1; // 1 ms
            timer.Tick += Timer_Tick;
        }

        public void Start(int duration)
        {
            timeLeft = duration;
            isRunning= true;
            isPaused = false;
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
            isRunning=false;
            isPaused = false;
        }

        public void Pause()
        {
            if (timer.Enabled)
            {
                timer.Stop();
                isRunning = false;
                isPaused = true;
            }
        }

        public void Resume()
        {
            if (isPaused)
            {
                timer.Start();
                isRunning = true;
                isPaused = false;
            }
        }

        public bool IsRunning()
        {
            return isRunning;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (timeLeft > 0)
            {
                timeLeft--;
                TimeChanged?.Invoke(timeLeft);
            }
            else
            {
                timer.Stop();
                TimerFinished?.Invoke();
            }
        }
    }
}
